package hidalgo.expenses.dao;

import hidalgo.expenses.entities.Account;
import hidalgo.expenses.entities.Note;
import hidalgo.expenses.entities.Session;
import hidalgo.expenses.entities.User;
import hidalgo.expenses.utils.Constants;
import hidalgo.expenses.utils.Crypto;
import hidalgo.expenses.utils.Util;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class UserDao extends BaseDao {

	public static String USER_ID = "user_id";
	public static String USERNAME= "username";
	public static String PASSWORD = "password";
	public static String EMAIL= "email";
	public static String EMAIL_VERIFIED = "emailverified";
	public static String VERIFICATION_CODE= "verificationcode";

	public static Session login(String username, String password) {

		Session session = null;
		try {

			String hashPassWord = Crypto.encrypt(Constants.SEED, password);
			
			MyHttpClient conection = new MyHttpClient(URLSELECT);

			/*
			conection.addDataToSend("queryphp", "CALL login('" + username
					+ "','" + hashPassWord + "',@token)");
			*/
			
			conection.addDataToSend("queryphp", "CALL sessionLogin('" + username
					+ "','" + hashPassWord + "',@id,@user,@token)");

			JSONObject jsonObjRecv = conection.executeHttpPost();

			if (jsonObjRecv != null) {

				JSONArray resultados = jsonObjRecv.getJSONArray("resultados");

				if (resultados != null) {

					for (int i = 0; i < resultados.length(); i++) {

						JSONObject elemento = (JSONObject) resultados.get(i);
						//Log.d("elemento!!!:O", "" + elemento.toString());
						
						session = new Session();
						session.setSessionId(elemento.getInt("session_id"));
						session.setUserId(elemento.getInt("user_id"));
						session.setToken(elemento.getString("token"));
						
					}

				}

			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return session;
	}

	public static boolean insertNewUser(User user) {

		Boolean success = false;

		MyHttpClient conection = new MyHttpClient(URLINSERT_UPDATE_DELETE);

		conection.addDataToSend(
				"queryphp",
				"INSERT INTO user " + "("
						+ USERNAME
						+ ","
						+ PASSWORD
						+ ","
						+ EMAIL
						+ ","
						+ VERIFICATION_CODE
						+ ") "
						+ " VALUES('"
						+ user.getName()
						+ "','"
						+ user.getPassword()
						+ "','"
						+ user.getEmail()
						+ "','"
						+ user.getVerificationCode()
						+ "')");

		JSONObject jsonObjRecv = conection.executeHttpPost();

		if (jsonObjRecv != null) {
			try {
				success = jsonObjRecv.getBoolean("resultados");
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		
		return success;
	}
	
	public static boolean verifyEmail(String email) {

		Boolean success = false;

		MyHttpClient conection = new MyHttpClient(URLINSERT_UPDATE_DELETE);

		String sql = " UPDATE user SET "
				+ EMAIL_VERIFIED + "='" + 1 + "'"
				+ " WHERE " + EMAIL + "= '"
				+ email +"' ";
		
		Log.d("hey!!!", sql);

		conection.addDataToSend("queryphp", sql);

		JSONObject jsonObjRecv = conection.executeHttpPost();

		if (jsonObjRecv != null) {
			try {
				
				success = jsonObjRecv.getBoolean("resultados");

				return success;
			} catch (JSONException e) {
				e.printStackTrace();
				return success;
			}
		} else {
			return success;
		}

	}
	
	public static User getUserById(int userId) {
		MyHttpClient conection = new MyHttpClient(URLSELECT);

		String sql ="SELECT * FROM user WHERE "
				+ USER_ID + "='" + userId + "' ";
		
		conection.addDataToSend("queryphp",sql);
		
		Log.d("query", ""+  sql);

		JSONObject jsonObjRecv = conection.executeHttpPost();

		if (jsonObjRecv != null) {
			try {
				JSONArray resultados = jsonObjRecv.getJSONArray("resultados");

				if (resultados != null) {

					User user = null;

					for (int i = 0; i < resultados.length(); i++) {

						JSONObject elemento = (JSONObject) resultados.get(i);

						user = new User();

						user.setUserId(elemento.getInt(USER_ID));
						user.setName(elemento.getString(USERNAME));
						user.setEmail(elemento.getString(EMAIL));
						user.setEmailVerified(elemento.getInt(EMAIL_VERIFIED)==1?true:false);
						user.setVerificationCode(elemento.getString(VERIFICATION_CODE));
						user.setPassword(elemento.getString(PASSWORD));

					}

					resultados = null;

					return user;
				} else {
					return null;
				}
			} catch (JSONException e) {
				e.printStackTrace();
				return null;
			}
		} else {
			return null;
		}
	}

	
}